﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class KhenThuong
    {
        public String ID;
        public String MaKhenThuong;
        public String TenGiaiThuong;
        public String HinhThucKhenThuong;
        public String CoQuanQuyetDinh;
        public DateTime NgayCap;
        public String NoiCap;

        public KhenThuong(String _ID)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM KhenThuong " +
                "WHERE ID = '" + _ID + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                ID = _ID;
                MaKhenThuong = row["MaKhenThuong"].ToString();
                TenGiaiThuong = row["TenGiaiThuong"].ToString();
                HinhThucKhenThuong = row["HinhThucKhenThuong"].ToString();
                CoQuanQuyetDinh = row["CoQuanQuyetDinh"].ToString();
                NgayCap = row.Field<DateTime>("NgayCap");
                NoiCap = row["NoiCap"].ToString();
            }
        }

        public KhenThuong(String _TenGiaiThuong, String _HinhThucKhenThuong,
            String _CoQuanQuyetDinh, DateTime _NgayCap, String _NoiCap)
        {
            ID = Database.getID("KhenThuong", "ID");
            MaKhenThuong = Database.getID("KhenThuong", "MaKhenThuong");
            TenGiaiThuong = _TenGiaiThuong;
            HinhThucKhenThuong = _HinhThucKhenThuong;
            CoQuanQuyetDinh = _CoQuanQuyetDinh;
            NgayCap = _NgayCap;
            NoiCap = _NoiCap;
        }
        public Boolean Save()
        {
            QueryResult result;
            //Insert
            result = Database.shareDatabase.Query("INSERT INTO KhenThuong VALUES(" +
                "'" + ID + "', " +
                "'" + MaKhenThuong + "', " +
                "N'" + TenGiaiThuong + "', " +
                "N'" + HinhThucKhenThuong + "', " +
                "N'" + CoQuanQuyetDinh + "', " +
                "'" + Database.sqlDateTime(NgayCap) + "', " + //Need to fix when possible
                "N'" + NoiCap + "')");
            if (result.ReturnCode == 200)
                return true;
            else
                return false;
        }
        public Boolean Update()
        {
            QueryResult result;
            if (Database.isDupplicate("KhenThuong", "ID", ID))
            {
                //Update
                result = Database.shareDatabase.Query("UPDATE KhenThuong SET " +
                    "TenGiaiThuong = N'" + TenGiaiThuong + "', " +
                    "HinhThucKhenThuong = N'" + HinhThucKhenThuong + "', " +
                    "CoQuanQuyetDinh = N'" + CoQuanQuyetDinh + "', " +
                    "NgayCap = '" + Database.sqlDateTime(NgayCap) + "', " +
                    "NoiCap = N'" + NoiCap + "' WHERE ID = '" + ID + "'");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
            return false;
        }
        public Boolean Delete()
        {
            QueryResult result = Database.shareDatabase.Query("DELETE FROM KhenThuong " +
                "WHERE ID = '" + ID + "'");
            if (result.ReturnCode == 200)
                return true;
            else
                return false;
        }

        public static List<KhenThuong> getAllKhenThuong(String _MaKhenThuong)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM KhenThuong " +
                "WHERE MaKhenThuong = '" + _MaKhenThuong + "'");
            if (result.ReturnCode == 200)
            {
                List<KhenThuong> _result = new List<KhenThuong>();
                foreach (DataRow dtr in result.Table.Rows)
                    _result.Add(new KhenThuong(dtr["ID"].ToString()));
                return _result;
            }
            else
                return null;
        }
    }
}
